\chapter{The state machine}
\label{chap:statemachine}
The state machine is the "heart" of the program.
It creates instances of all the needed controlling structures and implements the main state machine (see figure \ref{fig:statemachine}).

\section{Working principle}
The state machine works as seen on figure \ref{fig:statemachine}. In the states the controlling threads are activated and provided with the reference. It can be started, stopped and a reset via the user interface. 

\section{Implementation}
Each state is represented by a a class that implements the \textit{"state"} interface. This interface defines methods like a condition, the "next state" method and a execution method that is executed every cycle while the state is active. A wrapper class always holds a reference of the active state.
In the states the threads executing the action (for example controlling the ball) are woken up. Their status (for example "ball position reached") is used then in the condition method.
The state machine itself communicates with the Opcom by putting the values for the plotters and messages and receiving signals like start, stop or reset.
The cycle length is set to 10 milliseconds as no time critical action is done by this class.


\begin{figure}[hb]
	\centering
		\includegraphics[width=0.55\textwidth]{img/Statemachine.pdf}
	\caption{The state machine as it is implemented}
	\label{fig:statemachine}
\end{figure}
